# 检查 MySQL 服务
Write-Host "正在检查 MySQL 服务..." -ForegroundColor Green
$mysqlRunning = $false
try {
    $result = mysql -u root -p123456 -e "SELECT VERSION();" 2>$null
    if ($?) {
        $mysqlRunning = $true
        Write-Host "MySQL 服务正常运行" -ForegroundColor Green
    }
} catch {
    Write-Host "MySQL 服务未运行，请先启动 MySQL" -ForegroundColor Red
    exit
}

# 初始化数据库（如果需要）
Write-Host "正在检查数据库..." -ForegroundColor Green
$dbExists = $false
try {
    $result = mysql -u root -p123456 -e "USE travel_diary;" 2>$null
    if ($?) {
        $dbExists = $true
        Write-Host "数据库已存在" -ForegroundColor Green
    }
} catch {
    Write-Host "数据库不存在，正在初始化..." -ForegroundColor Yellow
    Get-Content init.sql | mysql -u root -p123456
    Write-Host "数据库初始化完成" -ForegroundColor Green
}

# 启动服务器
Write-Host "正在启动服务器..." -ForegroundColor Green
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd server; node server.js"
Write-Host "服务器已启动" -ForegroundColor Green

Write-Host "`n项目启动完成！" -ForegroundColor Green
Write-Host "请打开微信开发者工具，导入项目并运行" -ForegroundColor Yellow
Write-Host "记得在开发者工具中勾选'不校验合法域名...'选项" -ForegroundColor Yellow 